package jh.jianguan.springboot.util;

import jh.jianguan.springboot.Enum.BaseEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 前端返回json格式封装类
 * @author: guoc
 * @createDate: 2020/11/27 11:27
 */
@Data
public class ResultUtil extends Object implements Serializable {

    private static final long serialVersionUID = 1348172152215944560L;

    /**
    * 返回状态码，200为正确，100为失败
    */
    private int code;
    /**
     * 返回处理信息，成功或者失败
     */
    private String msg;
    /**
     * 成功返回true，失败返回false
     */
    private Boolean success;
    /**
     * 返回给前端的数据
     */
    private Map<String, Object> extend = new HashMap<String ,Object>();


    /**
     * 成功返回的json封装体
     * @return jh.jianguan.springboot.util.ResultUtil
     * @author guoc
     * @date 2020/11/27 11:35
     * value 原始数据
     */
    public static ResultUtil successJson(Object value){
        ResultUtil results = new ResultUtil();
        results.setCode(BaseEnum.SUCCESS.getIndex());
        results.setMsg(BaseEnum.SUCCESS.getMsg());
        results.setSuccess(true);
        results.getExtend().put("data",value);
        return results;
    }

    /**
     * 失败返回的json封装体
     * @return jh.jianguan.springboot.util.ResultUtil
     * @author guoc
     * @date 2020/11/27 12:44
     */
    public static ResultUtil errorJson(){
        ResultUtil results = new ResultUtil();
        results.setCode(BaseEnum.FAIL.getIndex());
        results.setSuccess(false);
        results.setMsg(BaseEnum.FAIL.getMsg());
        return results;
    }

    /**
     * 失败返回的json封装体
     * @return jh.jianguan.springboot.util.ResultUtil
     * @author guoc
     * @date 2020/11/27 11:37
     * msg 报错信息
     */
    public static ResultUtil errorJson(String msg){
        ResultUtil results = new ResultUtil();
        results.setCode(BaseEnum.FAIL.getIndex());
        results.setSuccess(false);
        results.setMsg(msg);
        return results;
    }

    /**
     * 失败返回的json封装体
     * @return jh.jianguan.springboot.util.ResultUtil
     * @author guoc
     * @date 2020/11/27 12:44
     */
    public static ResultUtil errorJson(String msg,Integer code){
        ResultUtil results = new ResultUtil();
        results.setCode(code);
        results.setSuccess(false);
        results.setMsg(msg);
        return results;
    }





}
